package cn.zhizihua.controller;

import cn.zhizihua.domain.po.Food;
import cn.zhizihua.domain.po.Food;
import cn.zhizihua.domain.query.FoodQuery;
import cn.zhizihua.domain.query.FoodQuery;
import cn.zhizihua.domain.vo.ResultBean;
import cn.zhizihua.exception.AppException;
import cn.zhizihua.exception.AppExceptionCodeMsg;
import cn.zhizihua.service.IFoodService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "餐品管理接口")
@RestController
@RequestMapping("/food")   //模块路径
public class FoodController{

    @Autowired
    IFoodService foodService;

    @ApiOperation("餐品添加或修改")
    @PostMapping      //  请求方式 + 请求路径   post  /模块路径[根据继承类型的模块路径来确定]
    public ResultBean save(@RequestBody Food food){
        System.out.println("----FoodController-----save--------");
        System.out.println(food);
        //添加或修改餐品信息
        boolean saveOrUpdate = foodService.saveOrUpdate(food);
        if (!saveOrUpdate){
            throw new AppException(AppExceptionCodeMsg.SAVE_OR_UPDATE_FAILE);
        }
        return ResultBean.SUCCESS();
    }

    @ApiOperation("根据餐品id删除餐品信息")
    @DeleteMapping("/{id}")   // http://localhost:8080/food/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------FoodController--------del-------------");
        System.out.println(id);
        //删除餐品
        boolean del = foodService.removeById(id);
        if (!del){
            throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    @ApiOperation("根据餐品id查询餐品信息")
    @GetMapping("/{id}")   // http://localhost:8080/user/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------FoodController--------getById-------------");
        System.out.println(id);
        //id查询用户信息
        Food byId = foodService.getById(id);
        System.out.println(byId);
        return ResultBean.SUCCESS(byId);
    }

    @ApiOperation("查询全部餐品信息")
    @GetMapping("/all")    //  get  /user/all
    public ResultBean getAll(){
        System.out.println("-------FoodController-----getAll--------");
        //获取全部数据
        List<Food> list = foodService.list();
        if (list.size()==0){
            throw new AppException(AppExceptionCodeMsg.GET_ALL_FAILE);
        }
        return ResultBean.SUCCESS(list);
    }

    @ApiOperation("分页查询餐品信息")
    @PostMapping("/page")    //  post  /xxx/all
    public  ResultBean getByPage(@RequestBody FoodQuery query){
        System.out.println("-------FoodController-----page--------");
        System.out.println(query);
        Page<Food> page = new Page<>(query.getPageNow(), query.getPageCount());
        foodService.page(page,query.getQueryWrapper());
        if (page.getTotal() == 0){
            throw new AppException(AppExceptionCodeMsg.GET_BY_PAGE_FAILE);
        }
        return ResultBean.SUCCESS(page);
    }

    @ApiOperation("批量删除餐品信息")
    @PostMapping("/batch")   // post + /xxx/batch
    public ResultBean batchDel(@RequestBody List<Integer> ids){
        System.out.println("----------FoodController---------batchDel---------------");
        boolean b = foodService.removeBatchByIds(ids);
        if (!b){
            throw new AppException(AppExceptionCodeMsg.BATCH_DEL_FAILE);
        }
        return ResultBean.SUCCESS();
    }

    @ApiOperation("根据商家id查询所属餐品信息")
    @GetMapping("/business/{bid}")
    public ResultBean getByBusinessId(@PathVariable("bid") Integer bid){
        System.out.println("----------FoodController---------getByBusinessId---------------");
        if (bid==null || bid==0){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        List<Food> foodList = foodService.getByBusinessId(bid);
        return ResultBean.SUCCESS(foodList);
    }

}
